package soar.test.json.fastjson;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

class Polymorphic
{
    public void run() throws Exception
    {
        String split = "------------------------------------------";

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("生成");
        System.out.println();

        Message message = new Message();
        message.setTitle("消息");
        List<Node> list = new ArrayList<>();
        NodeA node_a = new NodeA();
        node_a.setType(0);
        node_a.setId(100);
        node_a.setName("A100");
        list.add(node_a);
        NodeB node_b = new NodeB();
        node_b.setType(1);
        node_b.setAddress("上海");
        node_b.setSecureid("B200XXXXX");
        list.add(node_b);
        message.setList(list);

        String out_s = JSON.toJSONString(message);
        System.out.println(out_s);
        System.out.println(split);

        System.out.println("解析");
        System.out.println();

        Type[] types = new Type[] { NodeA.class, NodeB.class };
        JSON.parseArray(out_s, types);
        Message m = JSON.parseObject(out_s, Message.class);
        System.out.println("list: ");
        int count = 0;
        for (Node node : m.getList())
        {
            System.out.println("\t" + count + ":");

            if (node.getType() == 0)
            {
                NodeA na = (NodeA) node;
                System.out.println("\t\ttype: " + na.getType());
                System.out.println("\t\tid: " + na.getId());
                System.out.println("\t\tname: " + na.getName());
            }
            else if (node.getType() == 1)
            {
                NodeB nb = (NodeB) node;
                System.out.println("\t\ttype: " + nb.getType());
                System.out.println("\t\taddress: " + nb.getAddress());
                System.out.println("\t\tsecureid: " + nb.getSecureid());
            }
        }

        System.out.println(split);
    }
}

class Message
{
    private String title;
    private List<Node> list;

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    public List<Node> getList()
    {
        return list;
    }

    public void setList(List<Node> list)
    {
        this.list = list;
    }

}

// @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
// @JsonSubTypes({ @JsonSubTypes.Type(value = NodeA.class),
// @JsonSubTypes.Type(value = NodeB.class) })
class Node
{
    private int type;

    public int getType()
    {
        return type;
    }

    public void setType(int type)
    {
        this.type = type;
    }

}

class NodeA extends Node
{
    private int id;
    private String name;

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

}

class NodeB extends Node
{
    private String address;
    private String secureid;

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    public String getSecureid()
    {
        return secureid;
    }

    public void setSecureid(String secureid)
    {
        this.secureid = secureid;
    }

}